<html>
<head><meta charset="utf-8"><title>odd build.rs setup question · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html">odd build.rs setup question</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="242008339"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242008339" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242008339">(Jun 09 2021 at 01:30)</a>:</h4>
<p>Hello!</p>
<p>I am in a bit of a design pickle and im not sure how to structure this. So basically, i am writing a fluid engine which runs on the gpu as well as the cpu. For the gpu i have chosen to run cuda by writing my own cuda driver api lib that works on <code>.ptx</code> files. Those ptx files can be made by nvcc if i use <code>.cu</code> files, but they can also be made by rustc by targeting <code>nvptx64-nvidia-cuda</code>. Now, the issue is using <code>ptx-linker</code> does not work on windows due to something with llvm dylib limitations. So i found kind of a dirty hack around it, which is to just do <code>cargo +nightly rustc build --target nvptx64-nvidia-cuda -- --emit=asm</code>. This surprisingly makes a valid ptx file that works. So i have my main crate <code>onda</code> and my cuda crate <code>onda_cuda</code>. Then in the <a href="http://build.rs">build.rs</a> of onda i do this basically:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">read_dir</span><span class="p">(</span><span class="s">"../onda_cuda"</span><span class="p">).</span><span class="n">is_ok</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="fm">println!</span><span class="p">(</span><span class="s">"cargo:rerun-if-changed=../onda_cuda"</span><span class="p">);</span><span class="w"></span>

<span class="w">        </span><span class="c1">// download the nvptx64-nvidia-cuda target through rustup for nightly</span>
<span class="w">        </span><span class="c1">// build onda_cuda with the command i sent above</span>
<span class="w">        </span><span class="c1">// read from target/nvptx64-nvidia-cuda/release/deps/ and find the cuda-....s file</span>
<span class="w">        </span><span class="c1">// and move it to onda/resources/onda_cuda.ptx</span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>But im not sure if this is at all going to work if someone downloads the crate from <a href="http://crates.io">crates.io</a>, since it should not be trying to build the cuda crate since well, it doesnt have it, and it should have already been built into the ptx file when publishing. Any advice on maybe making this cleaner or is this the best ive got until i can use ptx-linker on windows?</p>



<a name="242019446"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242019446" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242019446">(Jun 09 2021 at 05:28)</a>:</h4>
<p>you could export a function from a crate that is added as dev dependency to the user crate and invoke it from the user crate's <a href="http://build.rs">build.rs</a>.</p>



<a name="242021044"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242021044" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242021044">(Jun 09 2021 at 06:01)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> i dont think that will work because i need my crate to be compileable on stable, but the cuda crate requires nightly for obvious reasons :(</p>



<a name="242021069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242021069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242021069">(Jun 09 2021 at 06:01)</a>:</h4>
<p>And i'd rather not go installing random targets on users' machines unless they are modifying the crate internally, i only need to rebuild the cuda crate when its modified</p>



<a name="242021140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242021140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242021140">(Jun 09 2021 at 06:02)</a>:</h4>
<p>How big is the ptx file? Maybe you can ship it as part of your crate?</p>



<a name="242021159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242021159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242021159">(Jun 09 2021 at 06:02)</a>:</h4>
<p>That's what im doing</p>



<a name="242021173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242021173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242021173">(Jun 09 2021 at 06:03)</a>:</h4>
<p>I build the ptx file, move it to the resources file, then include_str it</p>



<a name="242021188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242021188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242021188">(Jun 09 2021 at 06:03)</a>:</h4>
<p>but that's only done if the cuda crate is there, aka, this is being built by a developer, not a user</p>



<a name="242021259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242021259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242021259">(Jun 09 2021 at 06:04)</a>:</h4>
<p>Im mostly just ashamed of the <code>--emit=asm</code> hack i am doing, it's horrific</p>



<a name="242021274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242021274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242021274">(Jun 09 2021 at 06:04)</a>:</h4>
<p>I meant shipping it as part of the crate bundle on <a href="http://crates.io">crates.io</a>.</p>



<a name="242021312"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242021312" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242021312">(Jun 09 2021 at 06:05)</a>:</h4>
<p>i dont see the reason for doing that, it would require nightly to build it and the ptx only needs to be rebuilt if the cuda crate is changed</p>



<a name="242021465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242021465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242021465">(Jun 09 2021 at 06:08)</a>:</h4>
<p>and using the crate "normally" doesn't work because it relies on nvptx intrinsics, and even if you build it for that target, you need to either use ptx-linker or the --emit=asm hack to get usable ptx</p>



<a name="242023301"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242023301" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242023301">(Jun 09 2021 at 06:43)</a>:</h4>
<p>you understand me wrong I think. I meant shipping the precompiled ptx files.</p>



<a name="242025338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242025338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242025338">(Jun 09 2021 at 07:19)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> i dont delete them after i put them in /resources so technically they are shipped, i just include them instead of loading it at runtime. The thing im making is also a binary so im not sure if that would work...</p>



<a name="242025419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242025419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242025419">(Jun 09 2021 at 07:20)</a>:</h4>
<p>i can probably also get nvcc to compile it down to cubin to be smaller, but i dont think it will be very big</p>



<a name="242092188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242092188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242092188">(Jun 09 2021 at 16:20)</a>:</h4>
<p><span class="user-mention" data-user-id="276242">@Riccardo D'Ambrosio</span> FWIW, this is exactly the kind of use case that artifact dependencies are intended for: <a href="https://github.com/rust-lang/rfcs/pull/3028/">https://github.com/rust-lang/rfcs/pull/3028/</a></p>



<a name="242092359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242092359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242092359">(Jun 09 2021 at 16:21)</a>:</h4>
<p>We (the cargo team) could really use some help implementing that RFC.</p>



<a name="242092624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242092624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242092624">(Jun 09 2021 at 16:23)</a>:</h4>
<p>(That doesn't solve the problem of needing nightly, but it does make for a cleaner solution to depending on a binary from another target.)</p>



<a name="242134847"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242134847" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242134847">(Jun 09 2021 at 21:42)</a>:</h4>
<p>Oh lol I forgot I made that RFC</p>



<a name="242134860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242134860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242134860">(Jun 09 2021 at 21:43)</a>:</h4>
<p>Yes this is an ideal use case for it</p>



<a name="242135034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242135034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242135034">(Jun 09 2021 at 21:44)</a>:</h4>
<p>I had a use case today where I wanted to depend on a <em>proc macro</em> artifact</p>



<a name="242135052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/odd%20build.rs%20setup%20question/near/242135052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/odd.20build.2Ers.20setup.20question.html#242135052">(Jun 09 2021 at 21:44)</a>:</h4>
<p>presumably it's a good thing that those are excluded from the RFC, but still <span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>